package com.ruoyi.service.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.service.domain.ServiceCharge;
import com.ruoyi.service.mapper.ServiceChargeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.service.mapper.ServiceChargeReturnMapper;
import com.ruoyi.service.domain.ServiceChargeReturn;
import com.ruoyi.service.service.IServiceChargeReturnService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 退费记录Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-04-21
 */
@Service
public class ServiceChargeReturnServiceImpl implements IServiceChargeReturnService 
{
    private static final Logger log = LoggerFactory.getLogger(ServiceChargeReturnServiceImpl.class);

    @Autowired
    private ServiceChargeReturnMapper serviceChargeReturnMapper;
    @Autowired
    private ServiceChargeMapper serviceChargeMapper;
    /**
     * 查询退费记录
     * 
     * @param id 退费记录主键
     * @return 退费记录
     */
    @Override
    public ServiceChargeReturn selectServiceChargeReturnById(Long id)
    {
        return serviceChargeReturnMapper.selectServiceChargeReturnById(id);
    }

    /**
     * 查询退费记录列表
     * 
     * @param serviceChargeReturn 退费记录
     * @return 退费记录
     */
    @Override
    public List<ServiceChargeReturn> selectServiceChargeReturnList(ServiceChargeReturn serviceChargeReturn)
    {
        return serviceChargeReturnMapper.selectServiceChargeReturnList(serviceChargeReturn);
    }

    /**
     * 新增退费记录
     * 
     * @param serviceChargeReturn 退费记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertServiceChargeReturn(ServiceChargeReturn serviceChargeReturn)
    {
        serviceChargeReturn.setCreateTime(DateUtils.getNowDate());

        //更新收费单
        if(serviceChargeReturn.getChargeId() != null){
            ServiceCharge serviceCharge = serviceChargeMapper.selectServiceChargeById(serviceChargeReturn.getChargeId());
            if(serviceCharge == null){
                throw new ServiceException("收费单不存在");
            }

            if(serviceCharge.getFeeReturn() == null){
                serviceCharge.setFeeReturn(serviceChargeReturn.getFeeReturn());
            }else{
                serviceCharge.setFeeReturn(serviceCharge.getFeeReturn().add(serviceChargeReturn.getFeeReturn()));
            }
            if(serviceCharge.getFeeReturn().compareTo(serviceCharge.getFeeReal()) > 0){
                throw new ServiceException("退费金额不能大于实收金额");
            }
            serviceCharge.setStatus(serviceChargeReturn.getChargeStatus());
            serviceCharge.setUpdateBy(serviceChargeReturn.getCreateBy());
            serviceCharge.setUpdateTime(serviceChargeReturn.getCreateTime());
            serviceChargeMapper.updateServiceCharge(serviceCharge);
        }

        //保存退款记录
        return serviceChargeReturnMapper.insertServiceChargeReturn(serviceChargeReturn);
    }

    /**
     * 修改退费记录
     * 
     * @param serviceChargeReturn 退费记录
     * @return 结果
     */
    @Override
    public int updateServiceChargeReturn(ServiceChargeReturn serviceChargeReturn)
    {
        serviceChargeReturn.setUpdateTime(DateUtils.getNowDate());
        return serviceChargeReturnMapper.updateServiceChargeReturn(serviceChargeReturn);
    }

    /**
     * 批量删除退费记录
     * 
     * @param ids 需要删除的退费记录主键
     * @return 结果
     */
    @Override
    public int deleteServiceChargeReturnByIds(Long[] ids)
    {
        return serviceChargeReturnMapper.deleteServiceChargeReturnByIds(ids);
    }

    /**
     * 删除退费记录信息
     * 
     * @param id 退费记录主键
     * @return 结果
     */
    @Override
    public int deleteServiceChargeReturnById(Long id)
    {
        return serviceChargeReturnMapper.deleteServiceChargeReturnById(id);
    }
}
